﻿using System;

namespace Infrastructure.CrossCutting.Caching
{
    public static class CacheExtensions
    {
        public static T Get<T>(this ICache cache, CacheKey cacheKey)
        {
            object value = cache.Get(cacheKey);
            return value == null ? default(T) : (T)value;
        }

        public static T TryGet<T>(this ICache cache, CacheKey cacheKey) where T :class 
        {
            object value = cache.Get(cacheKey);
            return value == null ? default(T) : value as T;
        }

        public static T Get<T>(this ICache cache, string key)
        {
            return cache.Get<T>(new CacheKey(key));
        }

        public static T TryGet<T>(this ICache cache, string key) where T : class 
        {
            return cache.TryGet<T>(new CacheKey(key));
        }

        public static void Set(this ICache cache, string key, object value)
        {
            cache.Set(new CacheItemConfig(new CacheKey(key)), value);
        }

        public static void Set(this ICache cache, string key, object value, TimeSpan validFor)
        {
            cache.Set(new CacheItemConfig(new CacheKey(key), validFor), value);
        }

        public static void Remove(this ICache cache, string key)
        {
            cache.Remove(new CacheKey(key));
        }

        public static ICache Aggregate(this ICache cache, string aggregatekey)
        {
            return new AggregateCache(cache, aggregatekey);
        }
    }
}
